package com.jk.dmloginprovider.service;

import com.jk.dmcommon.entity.PageEntity;
import com.jk.dmloginprovider.dao.UserDao;
import com.jk.dmloginprovider.entity.AdminMoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import  com.jk.dmcommon.LybEntity.UserEntity;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    public String frontDeskLogin(UserEntity userEntity,HttpServletRequest request) {
    UserEntity userName = userDao.findUserInfo(userEntity);
       if (userName == null) {

            return "账号错误";
        } else {
            if (!userName.getUserPwd().equals(userEntity.getUserPwd())) {

                return "密码错误";
            } else {
                //测试session
                PageEntity pageEntity = new PageEntity();
                pageEntity.setOffset(11);
                pageEntity.setLimit(111);
                request.getSession().setAttribute("page",pageEntity);
                System.out.println(userName);
                request.getSession().setAttribute("user",userName);
                //存redis
                redisTemplate.opsForHash().put("dmLoginInfo","loginInfo",userName);
                redisTemplate.expire("dmLoginInfo",1800,TimeUnit.SECONDS);
                AdminMoEntity ad = new AdminMoEntity();
                ad.setWardenId(userName.getUserId());
                ad.setWardenNum(userName.getUserNum());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                ad.setWardenDate(sdf.format(new Date()));
                ad.setWardenOperate("用户"+userName.getUserName()+"登录了大麦网");

                return "登录成功";

            }
        }
    }


    public Object login1(UserEntity user, HttpServletRequest request) {
        Map map = new HashMap();
        String  attribute = "246887";

/*
        String attribute = request.getSession().getAttribute("checkcode").toString();
*/

        //把得到的验证码信息放到redis中                            实体类定义的字段用来存redis中 存的时间为60秒       时间的类型
        redisTemplate.opsForValue().set("yzm",attribute,60, TimeUnit.SECONDS);
        //后台查询手机号       不是用户名   sql语句select * from userr  where  userPhone=#{userPhone}
        Integer num = userDao.getUserPhone(user);
        //判断手机号
         if(num == 2){
            //手机号已经存在
            map.put("code",203);
        }
        else {
            //把存到redis中的验证码和前台输入的验证码做比较 是不是一致
            if (attribute.equals(user.getImgPath())) {

                map.put("code", 200);//创建成功
                user.setUserName("大麦用户");
                user.setUserImg("https://img.alicdn.com/bao/uploaded/i4/2251059038/O1CN01uQynGB2GdSLSn3X9b_!!2-item_pic.png_q60.jpg_.webp");
                userDao.addUserInfo(user);
            } else {
                map.put("code", 202);//验证码错误
            }
        }
        return map;
    }
}
